var app = angular.module('ZJPC');

app.controller('CarDetailController', ['$scope','$state','$rootScope','$stateParams','$location','DataService','$filter','CesiumService',CarDetailController]);

function CarDetailController($scope,$state,$rootScope,$stateParams,$location,DataService,$filter,CesiumService) {
	$scope.clearInput = function(e){
		$scope.search_entry = '';
	}

	$scope.searchList = []

	$scope.carInfo = DataService.deepCopy($stateParams.data);
	console.log(11,$stateParams.data)
	
	$scope.carInfo['cph'] = $stateParams.data.cph==undefined?$stateParams.data.carPlate:$stateParams.data.cph;

		//判断起始时间，额外考虑没有列表时间的情况
	if($stateParams.data.li!=null&&$stateParams.data.li!=undefined&&$stateParams.data.li.passTime!=undefined){
		$scope.starttime = new Date(Date.parse($stateParams.data.li.passTime.replace(/-/g,"/"))).setHours(0,0,0);
	}else if($scope.carInfo.passTime!=undefined){
		$scope.starttime = new Date(Date.parse($stateParams.data.passTime.replace(/-/g,"/"))).setHours(0,0,0)
	}else{
		$scope.starttime = new Date('2017-12-09').setHours(0,0,0);
	} 

	$scope.goBack = function(){
		history.back()
		$scope.$emit('clear')
	}

	$scope.searchTrack = function(){
		console.log(234)
		$scope.dataGroup = {}
		var starttime = new Date($scope.starttime).setHours(0,0,0)
		var endtime = new Date($scope.starttime).setHours(23,59,59)
		//这里需要依次查询3种轨迹、同行车、同行人的信
		$scope.loading = true;
		DataService.getCarTrack($filter('date')(starttime,'yyyy-MM-dd HH:mm:ss'),$filter('date')(endtime,'yyyy-MM-dd HH:mm:ss'),$scope.carInfo.cph,function(data){
			 CesiumService.destoryAll();
			$scope.loading = false;
			if(data!=null&&data!={}){
				$scope.dataGroup['track'] = data.track;
				$scope.dataGroup['ways'] = data.ways;
				var ways =  data.ways.ways.split(';');
				if(ways.length>0&&data.ways.ways!=""){
				var dataArray = []
					for(var i =0;i<ways.length-1;i++){
						dataArray.push({track:ways[i].split(',')});
					}
					var idGroup = CesiumService.getTrack($scope.dataGroup.ways.startTime,$scope.dataGroup.ways.endTime,dataArray, $scope.dataGroup.track,'track',$scope.carInfo.type)
					if(idGroup!=null){
						$scope.trackId = idGroup[0];
						$scope.dataGroup['trackId'] = idGroup;
						$scope.redPointId = idGroup[1];	
					}
				}
			}else{
				$scope.dataGroup['track'] = null;
			}
			$scope.$broadcast('detaildata',$scope.dataGroup)
			$scope.dataGroup_ = angular.fromJson($scope.dataGroup)
/*			$scope.chosen = 'track';*/
			/*$state.go('app.region.main.search.cardetail.track',{datag:$scope.dataGroup_})*/
		})
		DataService.getCarfellowcar($filter('date')(starttime,'yyyy-MM-dd HH:mm:ss'),$filter('date')(endtime,'yyyy-MM-dd HH:mm:ss'),'50','50',$scope.carInfo.cph,function(data2){
			$scope.dataGroup['fellowcar'] = data2;
			$scope.$broadcast('detaildata',$scope.dataGroup)
			$scope.dataGroup_ = angular.fromJson($scope.dataGroup)
/*			$scope.chosen = 'track';*/
			/*$state.go('app.region.main.search.cardetail.track',{datag:$scope.dataGroup_})*/
		})
		DataService.getPerfellowcar($filter('date')(starttime,'yyyy-MM-dd HH:mm:ss'),$filter('date')(endtime,'yyyy-MM-dd HH:mm:ss'),'50','50',$scope.carInfo.cph,function(data3){
			$scope.dataGroup['fellowper'] = data3;
			$scope.dataGroup['starttime'] = $scope.starttime;
			$scope.dataGroup['xm'] = $stateParams.data.cph;
			$scope.$broadcast('detaildata',$scope.dataGroup)
			$scope.dataGroup_ = angular.fromJson($scope.dataGroup)
/*			$scope.chosen = 'track';*/
			/*$state.go('app.region.main.search.cardetail.track',{datag:$scope.dataGroup_})*/
		})
	}

	$scope.baseInfo = function(){
		var url = $state.href('app.file.carfile.basic',{name:$scope.carInfo.cph});
	  	window.open(url,'_blank');
	}

	$scope.chosen = $location.path().split('/')[$location.path().split('/').length-1];

	$scope.clickChosen = function(a){
		$scope.chosen = a;
	}

	$scope.searchTrack();

	$scope.$watch('starttime',function(newValue,oldValue){
		if(newValue!=oldValue){
			console.log(newValue,oldValue)
			$scope.$broadcast('starttime',newValue)
			$scope.searchTrack()		
		}
	})

	$scope.$on("$destroy", function() {
        CesiumService.destoryAll();
    })

}